﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XPMVC.Database;

namespace XPMVC.Controllers
{
    public class AuctionController : Controller
    {
        private xpEntities db = new xpEntities();

        //
        // GET: /Auction/

        public ActionResult Index()
        {
            return View(db.Auction.ToList());
        }

        //
        // GET: /Auction/Details/5

        public ActionResult Details(int id = 0)
        {
            Auction auction = db.Auction.Find(id);
            if (auction == null)
            {
                return HttpNotFound();
            }
            return View(auction);
        }

        //
        // GET: /Auction/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Auction/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Auction auction)
        {
            if (ModelState.IsValid)
            {
                db.Auction.Add(auction);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(auction);
        }

        //
        // GET: /Auction/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Auction auction = db.Auction.Find(id);
            if (auction == null)
            {
                return HttpNotFound();
            }
            return View(auction);
        }

        //
        // POST: /Auction/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Auction auction)
        {
            if (ModelState.IsValid)
            {
                db.Entry(auction).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(auction);
        }

        //
        // GET: /Auction/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Auction auction = db.Auction.Find(id);
            if (auction == null)
            {
                return HttpNotFound();
            }
            return View(auction);
        }

        //
        // POST: /Auction/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Auction auction = db.Auction.Find(id);
            db.Auction.Remove(auction);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public bool Bet(int id, int bid, int user_id)
        {
            if ((ModelState.IsValid) && PutBet(id, bid))
            {
                var last_bid = db.Bidding.Where(x => x.art_piece.Equals(id)).OrderByDescending(y => y.id).Take(1).FirstOrDefault();

                if (last_bid != null)
                {
                    var last_bid_member = db.Member.Where(x => x.id.Equals(last_bid.bider)).FirstOrDefault();

                    last_bid_member.points += last_bid.current_bid;
                }

                db.Bidding.Add(new Bidding { art_piece = id, current_bid = bid, bider = user_id, bid_date = DateTime.Now });

                var user = db.Member.Where(x => x.id.Equals(user_id)).FirstOrDefault();

                user.points -= bid;

                return db.SaveChanges() > 0;
            }

            return false;
        }

        public bool PutBet(int id, int bid)
        {
            var current_bid = db.Bidding.Where(x => x.art_piece.Equals(id)).OrderByDescending(x => x.current_bid).FirstOrDefault();

            if (current_bid != null)
            {
                return bid > current_bid.current_bid;
            }
            else
            {
                return bid > 0;
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}